// pages/cont/cont.js
let WxParse = require("../../wxParse/wxParse.js")
const moment = require('../../moment/moment.min.js')
Page({
  data: {
    index: '',
    id: '',
    content: '',
    data: '',
    boo: ""
  },
  onLoad(options) {
    let that = this;
    that.setData({
      index: options.id
    })
    wx.request({
      url: 'https://cnodejs.org/api/v1/topics/',
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        that.setData({
          id: res.data.data[that.data.index].id,
        },()=>{
          wx.request({
            url: 'https://cnodejs.org/api/v1/topic_collect/qq1070760224',
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function (res) {
              if(res.data.data.length>0) {
                for (let i = 0; i < res.data.data.length; i++) {
                  if (res.data.data[i].id == that.data.id){
                    that.setData({
                      boo: false
                    })
                    break
                  }else{
                    that.setData({
                      boo: true
                    })
                  }
                }
              }
            }
          })
        })
        wx.request({
          url: 'https://cnodejs.org/api/v1/topic/' + that.data.id, 
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: function (res) {
            res.data.data.create_at = moment(res.data.data.create_at).locale('zh-cn').fromNow();
            for (let i = 0; i < res.data.data.replies.length; i++) {
              res.data.data.replies[i].create_at = moment(res.data.data.replies[i].create_at).locale('zh-cn').fromNow()
            }
            that.setData({
              data: res.data.data,
              content: res.data.data.content
            })
            WxParse.wxParse('articleContent', 'html', res.data.data.content, that, 5)
            for (let i = 0; i < that.data.data.replies.length; i++) {
              WxParse.wxParse('articleReplies' + i, 'html',that.data.data.replies[i].content, that)
              if (i === that.data.data.replies.length - 1) {
                WxParse.wxParseTemArray("replyTemArray", 'articleReplies', that.data.data.replies.length, that)
              }
            }
          }
        })
      },
      fail: function (res) {
      },
      complete: function (res) {
      }
    })
  },
  btn(){
    if(this.data.boo == true) {
      this.setData({
        boo: false 
      })
      wx.request({
        url: 'https://cnodejs.org/api/v1/topic_collect/collect',
        data: {
          accesstoken: "de4b3a69-1af4-47ee-aed9-cd7a43c2f0e5",
          topic_id: this.data.id
        },
        method: 'POST',
        dataType: 'json',
        responseType: 'text',
        success: function (res) {
          console.log(res)
        }
      })
    }else{
      this.setData({
        boo: true
      })
      wx.request({
        url: 'https://cnodejs.org/api/v1/topic_collect/de_collect',
        data: {
          accesstoken: "de4b3a69-1af4-47ee-aed9-cd7a43c2f0e5",
          topic_id: this.data.id
        },
        method: 'POST',
        dataType: 'json',
        responseType: 'text',
        success: function (res) {
          console.log(res)
        }
      })
    }
  }
})